package cn.stylefeng.guns.modular.order.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.goods.entity.LiuGoods;
import cn.stylefeng.guns.modular.goods.mapper.LiuGoodsMapper;
import cn.stylefeng.guns.modular.order.entity.*;
import cn.stylefeng.guns.modular.order.mapper.TbOrderMapper;
import cn.stylefeng.guns.modular.order.mapper.WeightPriceMapper;
import cn.stylefeng.guns.modular.order.model.params.TbFreightParam;
import cn.stylefeng.guns.modular.order.model.params.TbOrderParam;
import cn.stylefeng.guns.modular.order.model.result.TbFreightResult;
import cn.stylefeng.guns.modular.order.model.result.TbOrderResult;
import cn.stylefeng.guns.modular.order.service.TbFreightService;
import cn.stylefeng.guns.modular.order.service.TbOrderService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhou
 * @since 2019-08-02
 */
@Service
public class TbOrderServiceImpl extends ServiceImpl<TbOrderMapper, TbOrder> implements TbOrderService {
    @Autowired
    private TbFreightService tbFreightService;

    @Autowired
    private TbOrderMapper tbOrderMapper;

    @Autowired
    private LiuGoodsMapper liuGoodsMapper;

    @Autowired
    private WeightPriceMapper weightPriceMapper;

    @Override
    public void add(TbOrderParam param){
        param.setOrderId(IdUtil.simpleUUID());
        TbFreightParam freightParam = new TbFreightParam();
        freightParam.setArea(param.getArea());
        freightParam.setDestination(param.getDestination());
        TbFreightResult freightResult = tbFreightService.getFreightByAreaAndDestination(freightParam);
        LiuGoods goods = liuGoodsMapper.selectById(param.getGoodsId());
        BigDecimal weight = new BigDecimal(goods.getWeight());
        WeightPrice weightPrice = weightPriceMapper.selectById(freightResult.getWeightPriceId());
        BigDecimal fee;
        if (weight.compareTo(weightPrice.getFirstWeight()) > 0) {
            fee = weightPrice.getFirstWeight().multiply(weightPrice.getFirstPrice())
                    .add(weight.subtract(weightPrice.getFirstWeight()).multiply(weightPrice.getContinuesPrice()));
        } else {
            fee = weightPrice.getFirstPrice();
        }
        param.setPostFee(fee);
        TbOrder entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public TbOrderResult detail(TbOrderParam tbOrderParam) {
        return tbOrderMapper.detail(tbOrderParam);
    }

    @Override
    public void delete(TbOrderParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(TbOrderParam param){
        TbOrder oldEntity = getOldEntity(param);
        TbOrder newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public TbOrderResult findBySpec(TbOrderParam param){
        return null;
    }

    @Override
    public List<TbOrderResult> findListBySpec(TbOrderParam param){
        return null;
    }

    @Override
    public List<Map<String, Object>> getOrderList(String userId,String status) {
        return tbOrderMapper.getOrderList(userId,status);
    }

    @Override
    public LayuiPageInfo findPageBySpec(String param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public void deliver(TbOrderParam param) {
        param.setConsignTime(new Date());
        this.update(param);
    }


    private Serializable getKey(TbOrderParam param){
        return param.getOrderId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private TbOrder getOldEntity(TbOrderParam param) {
        return this.getById(getKey(param));
    }

    private TbOrder getEntity(TbOrderParam param) {
        TbOrder entity = new TbOrder();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public List<TbOrderApi> uploadDetail(List<String> myArray) {



        List<TbOrderApi> orders= tbOrderMapper.uploadDetail(myArray);
        List<Orderdetail> list=new ArrayList<>();
        list.add(new Orderdetail());

        for(TbOrderApi T  :orders){
            T.setOrderdetails(list);
            T.setEshopbuyer(new Eshopbuyer());
        }
        return orders;
    }


    @Override
    public void updateStatus(List<String> myArray,String status) {

       tbOrderMapper.updateStatus(myArray,status);

    }



    /**
     * 增加订单
     *
     */
    @Override
    public void addOrder(TbOrderParam param) {
        //TbOrder entity = getEntity(param);
       // this.save(entity);
        tbOrderMapper.addOrder(param);

    }

    /**
     * 删除订单
     *
     */
    @Override
    public void deleteOrders(TbOrderParam param) {
        //TbOrder entity = getEntity(param);
        // this.save(entity);deleteOrders
        tbOrderMapper.deleteOrders(param);

    }

    @Override
    public void updateS(TbOrderParam tbOrderParam) {
        tbOrderMapper.updateS(tbOrderParam);
    }

    @Override
    public Eshopbuyer selectWxUser(String id) {
        return tbOrderMapper.selectWxUser(id);
    }

    @Override
    public void updateOrders(TbOrderParam tbOrderParam) {
        tbOrderMapper.updateOrders(tbOrderParam);
    }




}
